var xmlHttp; //定义XMLHttpRequest对象
function createXmlHttpRequestObject() {
    //如果在internet Explorer下运行
    if (window.ActiveXObject) {
        try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
            xmlHttp = false;
        }

    } else {
        //如果在Mozilla或其他的浏览器下运行
        try {
            xmlHttp = new XMLHttpRequest();
        } catch (e) {
            xmlHttp = false;
        }
    }
    //返回创建的对象或显示错误信息
    if (!xmlHttp)
        alert("返回创建的对象或显示错误信息");
    else
        return xmlHttp;
}
createXmlHttpRequestObject();
document.addEventListener('DOMContentLoaded', function () {
    const buttons = document.querySelectorAll('.changebtn');

    buttons.forEach(function (button) {
        button.addEventListener('click', function () {
            form_id = this.parentElement.id;
            var form = document.getElementById(form_id);
            var docName = form.querySelector('.docname').innerHTML;
            var departname = form.querySelector('.departname').innerHTML;
            var doctorid = form.querySelector('.doctorid').value;
            var departid = form.querySelector('.departid').value;
            var changeDepart = form.querySelector('.select-change');
            var changeDepartValue = changeDepart.options[changeDepart.selectedIndex].value;
            var changename = changeDepart.options[changeDepart.selectedIndex].textContent;
            if (changeDepartValue == departid) {
                alert('修改科室与原科室相同！');
                return false;
            }
            if (changeDepartValue == "none") {
                alert('请选择科室！');
                return false;
            }
            if (confirm('确认把' + docName + '医生，从' + departname + '调整到' + changename + '？')) {
                xmlHttp.onreadystatechange = function () {
                    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                        alert('操作已提交！');
                        location.reload();
                    }
                } //判断URL调用的状态值并处理
                xmlHttp.open("GET", 'a_SubmitChangeDoctorDepartment.php?departid=' + changeDepartValue + '&docid=' + doctorid, false);
                xmlHttp.send(null);
            }
        });
    });
});